package com.bdqn.oa.dao.impl;

import com.bdqn.oa.dao.LeaveDao;
import com.bdqn.oa.entity.Leave;
import com.bdqn.oa.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Chenrz
 * @date 2022/7/20 10:48
 */
public class LeaveDaoImpl implements LeaveDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 新增请假条
     *
     * @param leave
     * @return
     */
    @Override
    public int saveLeave(Leave leave) throws SQLException {
        String sql = "insert into `leave` (employee_id,type,content,datetime,start_time,end_time,`status`)" +
                " values (?,?,?,?,?,?,?);";
        Object[] params = {
                leave.getEmployeeId(),leave.getType(),leave.getContent(),leave.getDatetime(),
                leave.getStartTime(),leave.getEndTime(),leave.getStatus()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 通过id删除请假条
     *
     * @param employeeId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteLeave(Integer employeeId) throws SQLException {
        String sql = "delete from `leave` where employee_id = ?;";
        return queryRunner.update(DruidUtils.getConnection(),sql,employeeId);
    }

    /**
     * 修改请假条
     *
     * @param leave
     * @return
     * @throws SQLException
     */
    @Override
    public int updateLeave(Leave leave) throws SQLException {
        String sql = "update `leave` set `type`=?,`content`=?,`datetime`=?,`start_time`=?,end_time=?,`status`=? where employee_id=?;";
        Object[] params = {
                leave.getType(),leave.getContent(),leave.getDatetime(),
                leave.getStartTime(),leave.getEndTime(),leave.getStatus(),leave.getEmployeeId()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 审批请假条
     *
     * @param leave
     * @return
     * @throws SQLException
     */
    @Override
    public int updateLeaveByStatus(Leave leave) throws SQLException {
        String sql = "update `leave` set `status`=?,`comment`=?,`comment_name`=?,`comment_datetime`=? where employee_id = ?;";
        Object[] params = {
                leave.getStatus(),leave.getComment(),leave.getCommentName(),leave.getCommentDatetime(),leave.getEmployeeId()
        };
        return queryRunner.update(DruidUtils.getConnection(),sql,params);
    }

    /**
     * 通过员工id查询请假条
     *
     * @param employeeId
     * @return
     * @throws SQLException
     */
    @Override
    public List<Leave> getListLeaveById(Integer employeeId) throws SQLException {
        List<Leave> leaveList = new ArrayList<Leave>();
        String sql = "select `id`,`employee_id`,`type`,`content`,`datetime`,`start_time`,`end_time`,`status`," +
                "`comment`,`comment_name`,`comment_datetime` from `leave` where `employee_id` = ?;";
        leaveList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Leave>(Leave.class),employeeId);
        return leaveList;
    }

    /**
     * 查询某一天的所有请假条
     *
     * @param datetime
     * @return
     * @throws SQLException
     */
    @Override
    public List<Leave> getListLeaveByDay(Date datetime) throws SQLException {
        List<Leave> leaveList = new ArrayList<Leave>();
        String sql = "select `id`,`employee_id`,`type`,`content`,`datetime`,`start_time`,`end_time`,`status`," +
                "`comment`,`comment_name`,`comment_datetime` from `leave` where `datetime` = ?;";
        leaveList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Leave>(Leave.class),datetime);
        return leaveList;
    }


    /**
     * 查询所有请假条
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Leave> getAllListLeave() throws SQLException {
        List<Leave> leaveList = new ArrayList<Leave>();
        String sql = "select `id`,`employee_id`,`type`,`content`,`datetime`,`start_time`,`end_time`,`status`," +
                "`comment`,`comment_name`,`comment_datetime` from `leave`";
        leaveList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Leave>(Leave.class));
        return leaveList;
    }
}
